package com.example.zhaojianxu.signin.common.util

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Criteria
import android.location.Location
import android.location.LocationManager
import android.provider.Settings
import android.support.v4.app.ActivityCompat
import android.text.TextUtils
import android.widget.Toast

/**
 * create by @zhaojianxu on 2019/2/12
 */
object LocationUtil {

    fun getBestLocation(context: Context): Location? {
        val c = Criteria();//Criteria类是设置定位的标准信息（系统会根据你的要求，匹配最适合你的定位供应商），一个定位的辅助信息的类
        c.setPowerRequirement(Criteria.POWER_LOW);//设置低耗电
        c.setAltitudeRequired(true);//设置需要海拔
        c.setBearingAccuracy(Criteria.ACCURACY_COARSE);//设置COARSE精度标准
        c.setAccuracy(Criteria.ACCURACY_LOW);//设置低精度
        //... Criteria 还有其他属性，就不一一介绍了
        val best = getBestLocation(context, c)
        if (best == null) {
            Toast.makeText(context, " best location is null", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(
                context,
                "best location: lat==" + best.getLatitude() + " lng==" + best.getLongitude(),
                Toast.LENGTH_SHORT
            ).show();
        }
        return best
    }

    fun getBestLocation(context: Context, criteria: Criteria?): Location? {
        var location: Location? = null
        val manager = (context.getSystemService(Context.LOCATION_SERVICE) as LocationManager)
        var mCriteria = criteria
        if (mCriteria == null) {
            mCriteria = Criteria()
        }
        val provider = manager.getBestProvider(mCriteria, true);
        if (TextUtils.isEmpty(provider)) {
            location = getGPSLocation(context)
        } else {
            //高版本的权限检查
            if (ActivityCompat.checkSelfPermission(
                    context,
                    Manifest.permission.ACCESS_COARSE_LOCATION
                ) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(
                    context,
                    Manifest.permission.ACCESS_FINE_LOCATION
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                return null
            }
            //获取最适合的定位方式的最后的定位权限
            location = manager.getLastKnownLocation(provider)
        }
        return location;
    }

    fun getGPSLocation(context: Context): Location? {
        var location: Location? = null
        val manager = (context.getSystemService(Context.LOCATION_SERVICE) as LocationManager)
        //高版本的权限检查
        if (ActivityCompat.checkSelfPermission(
                context,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            return null
        }
        if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
        }
        return location
    }

    fun openLocationService(activity: Activity){

        val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
        activity.startActivityForResult(intent, 10)
    }

    fun checkProvider(context: Context): Boolean{
        val manager = (context.getSystemService(Context.LOCATION_SERVICE) as LocationManager)
        return manager.isProviderEnabled(LocationManager.GPS_PROVIDER)
    }

}